package com.ruoyi.project.cms.controller;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cms.domain.CmsNews;
import com.ruoyi.project.cms.service.ICmsNewsService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 新闻管理Controller
 * 
 * @author ruoyi
 * @date 2020-03-19
 */
@RestController
@RequestMapping("/cms/news")
public class CmsNewsController extends BaseController
{
    @Autowired
    private ICmsNewsService cmsNewsService;

    /**
     * 查询新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:news:list')")
    @GetMapping("/list")
    public TableDataInfo list(CmsNews cmsNews)
    {
        startPage();
        List<CmsNews> list = cmsNewsService.selectCmsNewsList(cmsNews);
        return getDataTable(list);
    }

    /**
     * 导出新闻管理列表
     */
    @PreAuthorize("@ss.hasPermi('cms:news:export')")
    @Log(title = "新闻管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CmsNews cmsNews)
    {
        List<CmsNews> list = cmsNewsService.selectCmsNewsList(cmsNews);
        ExcelUtil<CmsNews> util = new ExcelUtil<CmsNews>(CmsNews.class);
        return util.exportExcel(list, "news");
    }

    /**
     * 获取新闻管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('cms:news:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(cmsNewsService.selectCmsNewsById(id));
    }

    /**
     * 新增新闻管理
     */
    @PreAuthorize("@ss.hasPermi('cms:news:add')")
    @Log(title = "新闻管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CmsNews cmsNews)
    {
        String username = SecurityUtils.getUsername();
        cmsNews.setCreateBy(username);
        cmsNews.setUpdateBy(username);
        return toAjax(cmsNewsService.insertCmsNews(cmsNews));
    }

    /**
     * 修改新闻管理
     */
    @PreAuthorize("@ss.hasPermi('cms:news:edit')")
    @Log(title = "新闻管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CmsNews cmsNews)
    {
        return toAjax(cmsNewsService.updateCmsNews(cmsNews));
    }

    /**
     * 删除新闻管理
     */
    @PreAuthorize("@ss.hasPermi('cms:news:remove')")
    @Log(title = "新闻管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cmsNewsService.deleteCmsNewsByIds(ids));
    }
}
